package com.ning.farm_income_back.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Land;
import com.ning.farm_income_back.domain.Users;
import com.ning.farm_income_back.dto.LandDto;
import com.ning.farm_income_back.mapper.LandMapper;
import com.ning.farm_income_back.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LandService {
    @Autowired
    LandMapper landMapper;

    @Autowired
    UsersMapper usersMapper;

    public PageInfo getLandInfo(LandDto land, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,"land_id desc");

        if(land.getAreaMax() == null){
            land.setAreaMax(10000);
        }
        if(land.getAreaMin() == null){
            land.setAreaMin(0);
        }

        List<LandDto> list = landMapper.selectBy(land);

        return new PageInfo<>(list);
    }


    public void saveLandInfo(Land land,String username) {
        //根据姓名查询用户id
        Users u = usersMapper.selectByUsername(username);
        //讲用户id赋值给land对象
        land.setOwnerId(u.getUserId());
        //添加land对象
        landMapper.insert(land);
    }

    public void updateLandInfo(Land land) {
        landMapper.updateByPrimaryKeySelective(land);
    }

    public void deleteLandInfo(Integer id) {
        landMapper.deleteByPrimaryKey(id.longValue());
    }

    public List<Land> getAllLand() {
        return  landMapper.selectAllLand();
    }
}
